<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 苗木自有属性
 */
class AttrController extends BaseController {

    /**
     * 自有属性列表
     */
    public function attrList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');

        $name = I('post.name', '');

        //参数数组
        $params = array();
        //构造WHERE条件
        $where = '1=1 ';
        if (!empty($name)) {
            $params['name'] = $name;
            $where .= "AND a.name LIKE '%{$name}%' ";
        }

        $attr = M('Attr');
        //数据总数
        $total = $attr->alias('a')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt1 ON tt1.id=a.type_id')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt2 ON tt2.id=tt1.pid')
                ->where($where)
                ->count();

        //数据列表
        $list = $attr->field('a.*,tt1.name AS small_name,tt2.name AS large_name')
                ->alias('a')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt1 ON tt1.id=a.type_id')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt2 ON tt2.id=tt1.pid')
                ->where($where)
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $treeAttrType = C('TREE_ATTR_TYPE');
                $item['attr_type'] = array_key_exists($item['attr_type'], $treeAttrType) ? $treeAttrType[$item['attr_type']] : '';
            }
            unset($item);
        }

        $this->assign('pageSize', $pageSize);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('orderField', $orderField);
        $this->assign('orderDirection', $orderDirection);
        $this->assign('total', $total);
        $this->assign('list', $list);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 添加自有属性
     */
    public function addAttr() {
        if (IS_POST) {
            $name = I('post.name', '');
            $small_type = I('post.small_type', 0);
            $attr_type = I('post.attr_type', 0);
            $value = I('post.value', '');
            $attr = M('Attr');

            if (empty($name)) {
                ajaxReturn('自有属性名称不能为空', 300);
            }
            if (empty($small_type)) {
                ajaxReturn('请选择苗木种类', 300);
            }
            if ($attr_type != 1) {
                if (empty($value)) {
                    ajaxReturn('属性类型除文本外,属性值必填', 300);
                }
            }

            $data = $attr->create();

            if ($data) {
                $data['type_id'] = $small_type;
                $ret = $attr->add($data);

                if ($ret) {
                    ajaxReturn('添加自有属性成功', 200, '', '', '', 'Admin/Attr/attrList');
                } else {
                    ajaxReturn('添加自有属性失败', 300);
                }
            } else {
                ajaxReturn('创建数据对象失败', 300);
            }
        } else {
            $treeAttrType = C('TREE_ATTR_TYPE');

            //获取苗木分类
            $largeType = M('TreeType')->field('id,name')->where("status=1 AND pid=0")->select();

            $this->assign('treeAttrType', $treeAttrType);
            $this->assign('largeType', $largeType);
            $this->display();
        }
    }

    /**
     * 修改自有属性
     */
    public function updAttr() {
        if (IS_POST) {
            $attrId = I('post.attrId', 0);
            $name = I('post.name', '');
            $small_type = I('post.small_type', 0);
            $attr_type = I('post.attr_type', 0);
            $value = I('post.value', '');
            $attr = M('Attr');

            if (empty($attrId)) {
                ajaxReturn('自有属性ID不能为空', 300);
            }
            if (empty($small_type)) {
                ajaxReturn('请选择苗木种类', 300);
            }
            if ($attr_type != 1) {
                if (empty($value)) {
                    ajaxReturn('属性类型除文本外,属性值必填', 300);
                }
            }

            $data = $attr->create();

            if ($data) {
                $data['type_id'] = $small_type;
                $ret = $attr->where("id={$attrId}")->save($data);

                if ($ret !== false) {
                    ajaxReturn('修改自有属性成功', 200, '', '', '', 'Admin/Attr/attrList');
                } else {
                    ajaxReturn('修改自有属性失败', 300);
                }
            } else {
                ajaxReturn('创建数据对象失败', 300);
            }
        } else {
            $attrId = I('get.attrId', 0);

            if (empty($attrId)) {
                ajaxReturn('自有属性ID不能为空', 300);
            }

            $attrInfo = M('Attr')->field('a.*,tt.pid AS large_type')
                    ->join('LEFT JOIN __TREE_TYPE__ AS tt ON tt.id=a.type_id')
                    ->alias('a')
                    ->where("a.id={$attrId}")
                    ->find();

            if (empty($attrInfo)) {
                ajaxReturn('没有找到该自有属性信息', 300);
            }

            $treeAttrType = C('TREE_ATTR_TYPE');

            //获取苗木分类
            $largeType = M('TreeType')->field('id,name')->where('status=1 AND pid=0')->select();

            //获取苗木种类
            $smallType = M('TreeType')->field('id,name')->where("status=1 AND pid={$attrInfo['large_type']}")->select();

            $this->assign('treeAttrType', $treeAttrType);
            $this->assign('largeType', $largeType);
            $this->assign('smallType', $smallType);
            $this->assign('attrInfo', $attrInfo);
            $this->display();
        }
    }

    /**
     * 删除自有属性
     */
    public function delAttr() {
        $ids = I('get.ids', '');

        if (empty($ids)) {
            ajaxReturn('请选择删除项', 300);
        }

        //删除自有属性
        $ret = M('Attr')->where("FIND_IN_SET(id,'{$ids}')")->delete();
        //删除苗木所有与该属性有关的值
        $ret2 = M('TreeAttr')->where("FIND_IN_SET(attr_id,'{$ids}')")->delete();

        if ($ret !== false && $ret2 !== false) {
            ajaxReturn('删除成功', 200, '', '', '', 'Admin/Attr/attrList');
        } else {
            ajaxReturn('删除失败', 300);
        }
    }
}
